package com.jinliang.blogweb.service.impl;

import com.jinliang.blogweb.entity.Article;
import com.jinliang.blogweb.entity.Label;
import com.jinliang.blogweb.mapper.ArticleMapper;
import com.jinliang.blogweb.service.ArticleService;
import com.jinliang.blogweb.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.Objects;

@Service
public class ArticleServiceImpl implements ArticleService {

    ArticleMapper articleMapper;
    LabelService labelService;
    public ArticleServiceImpl(LabelService labelService,ArticleMapper articleMapper) {
        this.labelService = labelService;
        this.articleMapper = articleMapper;
    }

    /**
     * 新增文章
     * @param article
     */
    @Override
    @Transient
    public void add(Article article) {
        /**
         * 标签逻辑：
         */
        if (Objects.nonNull(article.getLabel())) {
            Label label = labelService.getLabelByName(article.getLabel());
            if (Objects.isNull(label)) {
                label = new Label();
                label.setName(article.getLabel());
                labelService.add(label);
            }else {
                labelService.addLinkCount(label);
            }
        }
        /**
         * 新增文章
         */
        articleMapper.insert(article);

    }
}
